package com.itheima.service.impl.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.sms.SmsFlashPromotionSessionMapper;
import com.itheima.pojo.sms.SmsFlashPromotionProductRelation;
import com.itheima.pojo.sms.SmsFlashPromotionSession;
import com.itheima.pojo.sms.vo.SmsFlashPromotionSessionVo;
import com.itheima.service.sms.SmsFlashPromotionProductRelationService;
import com.itheima.service.sms.SmsFlashPromotionSessionService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class SmsFlashPromotionSessionServiceImpl extends ServiceImpl<SmsFlashPromotionSessionMapper, SmsFlashPromotionSession> implements SmsFlashPromotionSessionService {

    @DubboReference
    private SmsFlashPromotionProductRelationService flashPromotionProductRelationService;

    /**
     * 修改状态
     * @param id
     * @param status
     */
    public void updateStatus(Integer id, Integer status) {
        SmsFlashPromotionSession flashPromotionSession = this.getById(id);
        flashPromotionSession.setStatus(status);
        this.updateById(flashPromotionSession);
    }

    /**
     * 获取全部可选场次及其商品数量
     * @param flashPromotionId
     * @return
     */

    public List<SmsFlashPromotionSessionVo> selectList(Integer flashPromotionId) {

        List<SmsFlashPromotionSessionVo> flashPromotionSessionVoList = new ArrayList<>();

        //在商品限时购与商品关系表根据flashPromotionId获取全部可选场次id
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> flashPromotionProductRelationQueryWrapper = new LambdaQueryWrapper<>();
        flashPromotionProductRelationQueryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
        List<SmsFlashPromotionProductRelation> flashPromotionProductRelations = flashPromotionProductRelationService.list(flashPromotionProductRelationQueryWrapper);

        for (SmsFlashPromotionProductRelation flashPromotionProductRelation : flashPromotionProductRelations) {

            SmsFlashPromotionSessionVo flashPromotionSessionVo = new SmsFlashPromotionSessionVo();

            Long flashPromotionSessionId = flashPromotionProductRelation.getFlashPromotionSessionId();
            //查询启用场次表
            LambdaQueryWrapper<SmsFlashPromotionSession> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SmsFlashPromotionSession::getId, flashPromotionSessionId)
                    .eq(SmsFlashPromotionSession::getStatus,1);
            SmsFlashPromotionSession flashPromotionSession = this.getOne(lqw);

            if(flashPromotionSession != null){
                BeanUtils.copyProperties(flashPromotionSession, flashPromotionSessionVo);
                //封装商品数量
                flashPromotionSessionVo.setProductCount(flashPromotionProductRelation.getFlashPromotionCount());

                flashPromotionSessionVoList.add(flashPromotionSessionVo);
            }

        }


        return flashPromotionSessionVoList;
    }
}
